package com.trex.lwlsh.controlCenter;

import com.alibaba.fastjson.JSONObject;
import com.trex.lwlsh.dataDeal.CaseStepDeal;
import com.trex.lwlsh.dataDeal.LoadCase;
import com.trex.lwlsh.model.ConfigModel;
import com.trex.lwlsh.model.HtmlReport;
import com.trex.lwlsh.model.ModuleModel;
import com.trex.lwlsh.model.ReportModel;
import com.trex.lwlsh.util.ActionUtil;
import com.trex.lwlsh.util.LogUtil;
import org.testng.annotations.Test;

import java.util.ArrayList;

/**
 * Create by Trex on 2018/1/13
 */
public class Entrance {
    public ConfigModel configModel=new ConfigModel();
    public ArrayList<ModuleModel> moduleList=new ArrayList<ModuleModel> ();
    public LoadCase loadCase=new LoadCase();
    public CaseStepDeal csDeal=new CaseStepDeal();
    public ReportModel reportModel=new ReportModel();
    public HtmlReport htmlReport=new HtmlReport();
    public StringBuffer reportHtml=new StringBuffer();

    /**
     * 方法的入口
     */
    @Test
    public void main ()throws Exception{
        String ModuleStr="<div class=\"tab-content J_tabContent\" style=\"width:96%;overflow:hidden;margin:0 2%\"><div class=\"alert alert-warning\"><strong>【运行Case总数】:<span>sum</span> 【通过】: <span>TalPass</span> 【失败】: <span>TalFail</span> 【通过率】: <span>PassRate</span></strong></div>";
        System.out.println("trex:"+ModuleStr);
        //获取开始时间戳,毫秒
        long startTime = System.currentTimeMillis();
        //1加载要读取的配置文件
        String fileList[]=configModel.getFileList();
        //2根据需要的文件封装模块(里面封装所有的用例case)
        moduleList=loadCase.LoadCase(fileList);
        for (int i=0;i<moduleList.size();i++){
            long startModuleTime = System.currentTimeMillis();
            LogUtil.logs("【模块名称】: "+moduleList.get(i).getName());
            //3分解用例 单条执行
            System.out.println("===hello=="+moduleList.get(i));
            csDeal.ResolveModule(configModel,moduleList.get(i));
            //把结果写入report
            reportModel.TaskVO(moduleList.get(i).getPass(),moduleList.get(i).getFail(),moduleList.get(i).getSkip(),moduleList.get(i));
            //模块结束时间
            long overModuleTime = System.currentTimeMillis();
            moduleList.get(i).setDoTime((overModuleTime-startModuleTime)/1000);
            configModel.setTable(moduleList.get(i).getName(),moduleList.get(i));
            htmlReport.setTab(moduleList.get(i).getName(),i);
            System.out.println(moduleList.get(i)+"==asfas=====");
            reportHtml.append(htmlReport.getModule(configModel,moduleList.get(i)));

        }
        //一个文件运行结束时间
        long overTime = System.currentTimeMillis();
        reportModel.setRunTime((overTime-startTime)/1000);
        reportModel.ReportMap(configModel);
        JSONObject json=new JSONObject();
        json.put("result",reportModel);
        LogUtil.logs(json.toString());
        //结果数据处理
        ActionUtil.HtmlStr( configModel, reportModel, reportHtml, htmlReport);//结果数据处理


    }

}
